package com.ljkj.cordial.chat.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.bean.GroupSimpleBean;
import com.ljkj.cordial.chat.widget.PuzzleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NavRecycleGroupAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //显示头部
    private static final int VIEW_TYPE_ITEM_TITLE = 0;
    //显示内容
    private static final int VIEW_TYPE_ITEM_CONTENT = 1;
    private List<GroupSimpleBean> list = new ArrayList<>();

    public List<GroupSimpleBean> getList() {
        return list;
    }

    public void setList(List<GroupSimpleBean> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        this.list = list;
        notifyDataSetChanged();
    }

    public void clear() {
        if (!list.isEmpty()) {
            this.list.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        GroupSimpleBean pb = list.get(position);
        if (!TextUtils.isEmpty(pb.getTitle())) {
            return VIEW_TYPE_ITEM_TITLE;
        } else {
            return VIEW_TYPE_ITEM_CONTENT;
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == VIEW_TYPE_ITEM_TITLE) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_nav_group_title, null,false);
            return new ViewTitleHolder(view);
        }
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_nav_group, null,false);
        return new ViewGroupHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int i) {
        int viewType = getItemViewType(i);
        GroupSimpleBean item = getList().get(i);
        if (viewType == VIEW_TYPE_ITEM_TITLE) {
            ViewTitleHolder viewTitleHolder = (ViewTitleHolder) holder;
            viewTitleHolder.tvTitle.setText(item.getTitle());
        } else {
            ViewGroupHolder viewGroupHolder = (ViewGroupHolder) holder;
            viewGroupHolder.tvTitle.setText(item.getGroupName());
            if (!TextUtils.isEmpty(item.getMemberAvatarUrls()) && !TextUtils.isEmpty(item.getMemberUserIds())) {
                viewGroupHolder.piv.setImageBackGround(item.getMemberAvatarUrls(), item.getMemberUserIds());
            } else {
                viewGroupHolder.piv.setImageResource(R.mipmap.pic_group_avatar_default_58dp);
            }
            viewGroupHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onGroupItemClickListener != null) {
                        onGroupItemClickListener.onGroupItem(v, item, i);
                    }
                }
            });
        }
    }


    static class ViewTitleHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;

        ViewTitleHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static
    class ViewGroupHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.piv)
        PuzzleImageView piv;
        @BindView(R.id.tv_title)
        TextView tvTitle;

        ViewGroupHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    private OnGroupItemClickListener onGroupItemClickListener;

    public void setOnGroupItemClickListener(OnGroupItemClickListener onGroupItemClickListener) {
        this.onGroupItemClickListener = onGroupItemClickListener;
    }

    public interface OnGroupItemClickListener {
        void onGroupItem(View view, GroupSimpleBean item, int position);
    }
}
